const Category = require('../models/Category');
const { StatusCodes } = require('http-status-codes');
const mongoose = require('mongoose')
/**
 * 获取账目类别列表
 */
const getCategories = async (req, res) => {
   const categories = await Category.find().select('-__v');

   res.status(StatusCodes.OK).json({
      data: {
         list: categories,
      },
   });
};

/**
 * 创建分类
 */
const createCategory = async (req, res) => {
   const category = await Category.create(req.body);
   res.status(StatusCodes.OK).json({ msg: '创建成功', data: { category } });
};

/**
 * 更新分组信息
 */
const updateCategory = async (req, res) => {
   const categoryId = mongoose.Types.ObjectId(req.params.id);
   const category = await Category.findByIdAndUpdate(categoryId, req.body, {
      new: true,
      runValidators: true,
   });
   res.status(StatusCodes.OK).json({ msg: '更新成功', data: { category } });
};

/**
 * 删除分类
 */
const removeCategory = async (req, res) => {
   const categoryId = mongoose.Types.ObjectId(req.params.id);
   await Category.findByIdAndDelete(categoryId);
   res.status(StatusCodes.OK).json({ msg: '删除成功' });
};

module.exports = {
   getCategories,
   createCategory,
   updateCategory,
   removeCategory,
};
